require 'MVVM.Sources.View.Adaptor'

GrayChanger = class('GrayChanger')
function GrayChanger:ctor(obj, ChangeSelf, alpha)
    self.obj = obj
    self.ChangeSelf = ChangeSelf or false
    self.Alpha = alpha or 1
    self.TextList = {}
    self.ImageList = {}
    self.RawImageList = {}
    self.OutLineList = {}
    self.colorCache = {}

    self:Init()
end

function GrayChanger:getComponents(obj,compType)
    local ret = {}
    local comp = obj:GetComponent(compType)
    if comp and self.ChangeSelf then
        table.insert(ret, comp)
    end
    local compList = obj:GetComponentsInChildren(compType)
    for i=1,compList.Length do
        local comp = compList[i-1]
        table.insert(ret, comp)
    end
    return ret
end

function GrayChanger:Init()
    local _add = function(item,itemc)
        local instance = item:GetInstanceID()
        self.colorCache[instance] = itemc
    end

    local imgList = self:getComponents(self.obj,typeof(UnityEngine.UI.Image))
    for i=1,#imgList do
        local img = imgList[i]
        table.insert(self.ImageList, img)
        _add(img,img.color)
    end

    local textlist = self:getComponents(self.obj,typeof(UnityEngine.UI.Text))
    for i=1,#textlist do
        local txt = textlist[i]
        table.insert(self.TextList, txt)
        _add(txt,txt.color)
    end

    local imgList = self:getComponents(self.obj,typeof(UnityEngine.UI.RawImage))
    for i=1,#imgList do
        local img = imgList[i]
        table.insert(self.RawImageList, img)
        _add(img,img.color)
    end

    local outlinelist = self:getComponents(self.obj,typeof(UnityEngine.UI.Outline))
    for i=1,#outlinelist do
        local img = outlinelist[i]
        table.insert(self.OutLineList, img)
        _add(img,img.effectColor)
    end
end

function GrayChanger:ChangeColor(flag)
    if flag then
        for _,data in pairs(self.ImageList) do
            data.color = self.colorCache[data:GetInstanceID()]
        end

        for _,data in pairs(self.TextList) do
            data.color = self.colorCache[data:GetInstanceID()]
        end

        for _,data in pairs(self.RawImageList) do
            data.color = self.colorCache[data:GetInstanceID()]
        end

        for _,data in pairs(self.OutLineList) do
            data.effectColor = self.colorCache[data:GetInstanceID()]
        end
    else
        for _,data in pairs(self.ImageList) do
            data.color = Color.New(0,0,0,self.Alpha)
        end

        for _,data in pairs(self.TextList) do
            data.color = Color.New(0.5,0.5,0.5,self.Alpha)
        end

        for _,data in pairs(self.RawImageList) do
            data.color = Color.New(0,0,0,self.Alpha)
        end

        for _,data in pairs(self.OutLineList) do
            data.effectColor = Color.New(0.2,0.2,0.2,self.Alpha)
        end
    end
end

function GrayChanger:ReInit()
    self.TextList = {}
    self.ImageList = {}
    self.RawImageList = {}
    self.OutLineList = {}

    self.colorCache = {}
    self:Init()
end

GrayAdaptor = class('GrayAdaptor', Adaptor)
function GrayAdaptor:ctor(obj, propertyName, binder, func,ChangeSelf,alpha)
    self.func = func

    self.changer = GrayChanger.new(obj, ChangeSelf, alpha)
    self:BindProperty(propertyName, binder)
end

function GrayAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if self.func then
        self:ChangeColor(self.func(newValue))
    else
        self:ChangeColor(newValue)
    end
end

function GrayAdaptor:ChangeColor(flag)
    self.changer:ChangeColor(flag)
end

function GrayAdaptor:ReInit()
    self.changer:ReInit()
end